package com.dangf.hzapp.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.FragmentTransaction;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dangf.hzapp.R;
import com.dangf.hzapp.activity.MainActivity;
import com.dangf.hzapp.adapter.MenuAdapter;
import com.dangf.hzapp.entity.User;
import com.dangf.hzapp.util.Image;


/**
 * 菜单布局
 * @author 韦海生
 * @date 2014年8月4日
 */
public class MenuFragment extends BaseFragment {
	private View menu_layout;	//菜单布局文件
	
	private ImageView user_photo;	//用户头像
	private TextView user_name;		//用户名
	private ListView menu_list;		//菜单列表
	private String[] menu = { "我的账单", "我的房间", "关于我们", "退出" };
	
	FragmentTransaction transaction;
	
	private MenuAdapter adapter;
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		if(menu_layout==null)menu_layout = inflater.inflate(R.layout.fragment_menu, container, false);
		
		initLayout();	//初始化控件
		loadData();		//加载数据
		initListener();	//初始化监听器
		
		return menu_layout;
	}

	//初始化监听器
	private void initListener() {
		//菜单选择监听器
		menu_list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

				switch (menu[position]) {
				case "退出":
					app.outApp(getActivity());
					break;
				case "关于我们":
					transaction = getFragmentManager().beginTransaction();
					transaction.replace(R.id.content_frame,new AboutFragment());
					transaction.addToBackStack(null);
					transaction.commit();
					break;
				}
				
				//关闭
				MainActivity.drawerLayout.closeDrawer(Gravity.LEFT);
			}
		});
	}

	//加载数据
	private void loadData() {
		Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.start);
		user_photo.setImageBitmap(Image.getRoundedCornerBitmap(bitmap,2));
		
		User u = com.dangf.hzapp.Config.getCacheToken(getActivity());
		if(!"".equals(u.getName()))user_name.setText(u.getName());
		else user_name.setText(u.getUsername());

		adapter = new MenuAdapter(getActivity(), menu);
		menu_list.setAdapter(adapter);
	}

	//初始化控件
	private void initLayout() {
		user_photo = (ImageView) menu_layout.findViewById(R.id.user_photo);
		user_name = (TextView) menu_layout.findViewById(R.id.user_name);
		menu_list = (ListView) menu_layout.findViewById(R.id.menu_list);
	}
	
	
}
